<?php
/**
 * Created by PhpStorm.
 * Script Name: MiniTemplate.php
 * Create: 2023/2/6 16:34
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;
use plugin\wechat\app\model\MiniTemplateApp as TemplateAppM;

class MiniTemplateApp extends Mini
{
    static $model = null;

    static function model(){
        if(is_null(self::$model)){
            self::$model = new TemplateAppM();
        }
        return self::$model;
    }

    static function getByApp($app_name = ''){
        return self::model()->where('app_name', $app_name)
            ->find();
    }

    /**
     * 新增或更新
     * @param string $app_name
     * @param int $template_id
     * @return array|mixed|\think\db\Query|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function updateByApp($app_name = '', $template_id = 0){
        $data = self::model()->where('app_name', $app_name)
            ->find();
        if($data){
            $data = self::model()->update(['id' => $data['id'], 'template_id' => $template_id]);
        }else{
            $data = self::model()->createOne(['app_name' => $app_name, 'template_id' => $template_id]);
        }
        return $data;
    }
}